package t1invoke;

import java.util.List;
import java.sql.SQLException;
import java.util.Scanner;

import t1bean.MajorBean;
import t1bean.StudentBean;
import t1dao.MajorActionDao;
import t1dao.StudentActionDao;

/**
 * 调用各dao类
 *
 */
public class Invoke {
	private StudentActionDao sa = new StudentActionDao();
	private MajorActionDao ma = new MajorActionDao();

	/**
	 * @throws SQLException select * from student 全部查出并输出
	 */
	public void selectAll() throws SQLException {
		MajorBean major = new MajorBean();
		List<StudentBean> stuList = sa.selectAll();
		if (stuList.get(0).getId() == null) {
			System.out.println("no infromation");
			return;
		}
		for (StudentBean stu : stuList) {
			System.out.println("姓名:" + stu.getName());
			System.out.println("学号:" + stu.getNumber());
			major.setNumber(stu.getMajorNumber());
			for (MajorBean major2 : ma.select(major)) {
				System.out.println("专业:" + major2.getName());
			}
			System.out.println();
//			major=ma.select(major).get(0);
//			System.out.println("专业:"+major.getName());
		}
	}

	/**
	 * @param sc
	 * @throws SQLException 
	 * select * from student where name=？
	 */
	public void selectStudentByName(Scanner sc) throws SQLException {
		StudentBean student = new StudentBean();
		MajorBean major = new MajorBean();
		System.out.println("输入姓名");
		String name = sc.nextLine();
		if (name.equals(" ") || name.equals("")) {
			System.out.println("输入错误");
			return;
		}
		student.setName(name);
		List<StudentBean> stuList = sa.select(student);
		if (stuList.get(0).getId() == null) {
			System.out.println("can not find!");
		}
		for (StudentBean stu : stuList) {
			System.out.println("姓名:" + stu.getName());
			System.out.println("学号:" + stu.getNumber());
			major.setNumber(stu.getMajorNumber());
			for (MajorBean major2 : ma.select(major)) {
				System.out.println("专业:" + major2.getName());
			}
			System.out.println();
		}
	}

	/**
	 * @param sc
	 * @throws SQLException
	 * 
	 *                      select major_id from major where major_name=? select *
	 *                      from student where major_id=?
	 * 
	 */
	public void selectStudentByMajor(Scanner sc) throws SQLException {
		System.out.println("输入专业");
		String majorName = sc.nextLine();
		MajorBean major = new MajorBean();
		StudentBean student = new StudentBean();
		major.setName(majorName);
		List<MajorBean> maList = ma.select(major);
		if (maList.get(0).getId() == null) {
			System.out.println("输入错误");
			return;
		}
		student.setMajorNumber(maList.get(0).getNumber());
		List<StudentBean> stuList = sa.select(student);
		if (stuList.get(0).getId() == null) {
			System.out.println("无学生");
			return;
		}
		for (StudentBean stu : stuList) {
			System.out.println("姓名:" + stu.getName());
			System.out.println("学号:" + stu.getNumber());
			System.out.println("专业:" + majorName);
			System.out.println();
		}
	}

	/**
	 * @param sc
	 * @throws SQLException 
	 * insert into student(name,number,major_id) values(?,?,?)
	 */
	public void insertStudent(Scanner sc) throws SQLException {
		System.out.println("输入姓名");
		String name = sc.nextLine();
		System.out.println("输入学号");
		Integer number = Integer.parseInt(sc.nextLine());
		System.out.println("输入专业");
		String majorName = sc.nextLine();
		MajorBean major = new MajorBean();
		major.setName(majorName);
		List<MajorBean> list = ma.select(major);
		if (list.size() == 0) {
			System.out.println("专业不存在");
			return;
		}
		StudentBean student = new StudentBean(number, name, list.get(0).getNumber());
		sa.insert(student);
		System.out.println("添加成功");
	}
}
